package com.wswy.wyjk.ui.main;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.my.httpapi.api.annotation.ViewL;
import com.my.httpapi.api.baseUtils.Toast;
import com.my.httpapi.api.baseView.BaseActivity;
import com.tencent.mmkv.MMKV;
import com.wswy.wyjk.R;
import com.wswy.wyjk.api.Api;
import com.wswy.wyjk.api.MMKVConstant;
import com.wswy.wyjk.api.entity.RequestParameter;
import com.wswy.wyjk.core.App;
import com.wswy.wyjk.model.PracticeType;
import com.wswy.wyjk.model.SubjectType;

import com.wswy.wyjk.ui.common.Router;
import com.wswy.wyjk.ui.dialog.SettingSubjectUpdateDialog;
import com.wswy.wyjk.ui.login.LoginActivity;
import com.wswy.wyjk.ui.main.choose.PracticeTypeActivity;
import com.wswy.wyjk.ui.main.choose.PracticeTypeNewActivity;
import com.wswy.wyjk.ui.main.event.CarType;
import com.wswy.wyjk.ui.main.event.InfoEvent;
import com.wswy.wyjk.ui.main.model.ErrResponse;

import com.wswy.wyjk.ui.practiceNew.ErrTopicActivity;
import com.wswy.wyjk.ui.practiceNew.ExamRecordActivity;

import com.wswy.wyjk.utils.GlideUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

@ViewL(R.layout.layout_mine)
public class MineActivity extends BaseActivity {

    private TextView tvClosed, tvType, tvExamRecord, tvRank, tvQuestionUpdate, tvWrong, tvName, tvDescribe, tvPraise, tvCollection;
    private View rlMineTop, tvSetting, tvFeedback, llType;
    private ImageView ivHead;

    public static int subjectType;
    private String token;
    private MMKV mmkv;

    public static String sqlValue;

    @Override
    protected void initData(Bundle savedInstanceState) {
        initView();
        initData();
    }

    @Override
    protected String showTitle() {
        return null;
    }

    @Override
    protected boolean statusBarIsBlack() {
        return true;
    }

    @Override
    protected void destroy() {
        EventBus.getDefault().unregister(this);
    }

    private void initView() {
        tvRank = findViewById(R.id.tv_rank);
        tvClosed = findViewById(R.id.tv_closed);
        rlMineTop = findViewById(R.id.rl_mine_top);
        tvSetting = findViewById(R.id.tv_setting);
        tvFeedback = findViewById(R.id.tv_feedback);
        llType = findViewById(R.id.ll_type);
        tvType = findViewById(R.id.tv_type);
        tvExamRecord = findViewById(R.id.tv_exam_record);
        tvQuestionUpdate = findViewById(R.id.tv_question_update);
        tvWrong = findViewById(R.id.tv_wrong);
        tvName = findViewById(R.id.tv_name);
        tvDescribe = findViewById(R.id.tv_describe);
        ivHead = findViewById(R.id.iv_head);
        tvPraise = findViewById(R.id.tv_praise);
        tvCollection = findViewById(R.id.tv_collection);
    }

    private void initData() {
        EventBus.getDefault().register(this);
        mmkv = MMKV.defaultMMKV();
        tvType.setText(getString(R.string.current_practice_type, App.getPracticeContext().licenseType.title));
        token = mmkv.decodeString(MMKVConstant.TOKEN);
        if (!TextUtils.isEmpty(token)) {
            updateUI();
        }
        tvClosed.setOnClickListener(view -> finish());
        rlMineTop.setOnClickListener(view -> {
            if (!TextUtils.isEmpty(token)) {
                return;
            }
            intent(LoginActivity.class);
        });
        tvSetting.setOnClickListener(view -> intent(SettingActivity.class));
        tvFeedback.setOnClickListener(view -> gotoWebView("意见反馈"));
        llType.setOnClickListener(view -> intent(PracticeTypeActivity.class));
        tvRank.setOnClickListener(view -> intent(RankActivity.class));
        tvExamRecord.setOnClickListener(view -> intent(ExamRecordActivity.class));
        tvQuestionUpdate.setOnClickListener(view -> {
            //插入数据的sql
            // App.getDaoSession().AddData(new RecordDto());
            new SettingSubjectUpdateDialog(MineActivity.this).builder().show();
        });
        tvWrong.setOnClickListener(view -> {
//            requestErr();
            Map<String, Object> map = new HashMap<>();
            map.put("type", "错题");
            intent(ErrTopicActivity.class, map);
        });
        tvCollection.setOnClickListener(view -> {
            Map<String, Object> map = new HashMap<>();
            map.put("type", "收藏");
            intent(ErrTopicActivity.class, map);
        });
        tvPraise.setOnClickListener(view -> {
            try {
                Uri uri = Uri.parse("market://details?id=" + getPackageName());
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            } catch (Exception e) {
                Toast.show("您的手机没有安装Android应用市场");
                e.printStackTrace();
            }
        });
    }

    private void gotoWebView(String type) {
        Map map = new HashMap();
        map.put("type", type);
        intent(WebViewActivity.class, map);
    }

    private void updateUI() {
        tvName.setText(mmkv.decodeString(MMKVConstant.NICKNAME));
        tvDescribe.setText("管理个人账户信息");
        GlideUtils.load(this, mmkv.decodeString(MMKVConstant.HeadImg), ivHead, true);
    }

    private void requestErr() {
        Map<String, String> map = RequestParameter.generate().getSingedParams();
        map.put("", "");
        subscribe(Api.getApi().getErr(map),
                request(data -> getErrData((ErrResponse) data), true));
    }

    private void getErrData(ErrResponse errResponse) {
        //拼接sql
        sqlValue = "( 800000 , 800100 )";
        PracticeType practiceType = new PracticeType();
        practiceType.subjectType = SubjectType.KEMU1;
        practiceType.licenseType = App.getPracticeContext().licenseType;
        practiceType.areaCode = App.getPracticeContext().getAreaCode();
        practiceType.type = PracticeType.TYPE_ERROR;
        Router.start(this, Router.PRACTICE, null, practiceType);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateType(CarType carType) {
        tvType.setText(getString(R.string.current_practice_type, carType.getType()));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateInfo(InfoEvent infoEvent) {
        token = mmkv.decodeString(MMKVConstant.TOKEN);
        updateUI();
    }
}
